package com.marketlive.entity.account;

import com.marketlive.entity.EntityHome;
import com.marketlive.entity.IntegerPk;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import org.marketlive.entity.IEntity;
import org.marketlive.entity.IPrimaryKey;
import org.marketlive.entity.account.IAccountSet;
import org.marketlive.entity.account.IContact;
import org.marketlive.entity.account.IContactHome;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @inheritDoc
 */
public class ContactHome extends EntityHome implements IContactHome {

    /**
     * Default contact for shipments.  Lazily created.
     */
    private IContact defaultShipToContact = null;


    /**
     * Instantiate a Contact Home object, along with corresponding page objects.
     * 
     * @throws Exception if can not create the home object
     */
    public ContactHome() throws Exception {
        super(Contact.ENTITY_TYPE, Contact.class);
        Contact.setEntityHome(this);
    }

    /**
     * @inheritDoc
     */
    public IPrimaryKey getPk(final String pkString) {
        int pkInt = -1;
        try {
            pkInt = Integer.parseInt(pkString);
        } catch (NumberFormatException e) {
            throw new RuntimeException("pkString for Contact must be an integer.");
        }
        return new IntegerPk("contact", pkInt);
    }

    /**
     * @inheritDoc
     */
    public IEntity get(final IPrimaryKey pk) {
        Contact a = new Contact();
        a.setPk(pk);
        return a;
    }

    /**
     * @inheritDoc
     */
    public IContact create(final String alias) {
        //Contact contact = new Contact(alias,
        //                              new Person("person", "", "", ""),
        //                              new Address("address", "", "", "", null, null));
        Contact contact = new Contact();
        super.create(contact);
        return (IContact) contact;
    }

    /**
     * @inheritDoc
     */
    public Collection findByAlias(final String alias) {
        return getHibernateTemplate().find("from Contact where alias = ?", alias);
    }

    /**
     * @inheritDoc
     */
    public IContact findByCode(final String code) {
        
        List contacts = getHibernateTemplate().find("from Contact where delete_flag = 0 and code = ?", code);
        if (contacts.isEmpty()) {
            return null;
        }
        if (contacts.size() > 1) {
            throw new IllegalArgumentException("More than one contact was found with code: " + code);
        }
        return (IContact)contacts.get(0);
    }

    /**
     * @inheritDoc
     * @deprecated as of MarketLive 5.5 without replacement. Will be removed in the next major release since 
     * soft delete will not be supported.
     */
    public void markDeleted(final IContact contact) {
        contact.setDateModified(getTimestamp());
        contact.setDateDeleted(getTimestamp());
        contact.setDeleted(true);
        getHibernateTemplate().update(contact);
    }

    /**
     * Find all contacts that are not marked as deleted.
     * @return Collection of Contact entities.
     */
    public Collection findAllNonDeleted() {
        return getHibernateTemplate().find("from Contact where delete_flag = 0");
    }

    /**
     * @inheritDoc
     */
    public List findByCustomerPk(IPrimaryKey pk) {
        List contactList = new ArrayList();
        //todo implement this finder.
        //this can be done using the customer.getSkus() call in customer entity.
        return contactList;
    }

    /**
     * @inheritDoc
     */
    public IContact getDefaultShipToContact() {
        if (defaultShipToContact==null) {
            synchronized (this) {
                if (defaultShipToContact==null) {
                    defaultShipToContact = (IContact) get(new IntegerPk("contact", -1));
                }
            }
        }
        return defaultShipToContact;
    }

}


